/**
 * localStorage 存贮
 * 目前对象值如果是函数 、RegExp等特殊对象存贮会被忽略
 * @param {String} key  属性
 * @param {Object} value 值
 */
export function localStorageSet(key, value) {
	if (typeof(value) === 'object') value = JSON.stringify(value)
	localStorage.setItem(key, value)
}

/**
 * localStorage 获取
 * @param {String} key  属性
 */
export const localStorageGet = (key) => {
	return localStorage.getItem(key)
}

/**
 * localStorage 移除
 * @param {String} key  属性
 */
export const localStorageRemove = (key) => {
	localStorage.removeItem(key)
}

/**
 * localStorage 移除所有
 * @param {String} key  属性
 */
export const localStorageClear = () => {
	localStorage.clear()
}

/**
 * localStorage 存贮某一段时间失效
 * @param {String} key  属性
 * @param {*} value 存贮值
 * @param {String} expire 过期时间,毫秒数
 */
export const localStorageSetExpire = (key, value, expire) => {
	if (typeof(value) === 'object') value = JSON.stringify(value)
	localStorage.setItem(key, value)
	setTimeout(() => {
		localStorage.removeItem(key)
	}, expire)
}

/**
 * sessionStorage 存贮
 * @param {String} key  属性
 * @param {*} value 值
 */
export const sessionStorageSet = (key, value) => {
	if (typeof(value) === 'object') value = JSON.stringify(value)
	sessionStorage.setItem(key, value)
}

/**
 * sessionStorage 获取
 * @param {String} key  属性
 */
export const sessionStorageGet = (key) => {
	return sessionStorage.getItem(key)
}

/**
 * sessionStorage 删除
 * @param {String} key  属性
 */
export const sessionStorageRemove = (key, value) => {
	sessionStorage.removeItem(key, value)
}

/**
 * sessionStorage 移除所有
 * @param {String} key  属性
 */
export const sessionStorageClear = () => {
	sessionStorage.clear()
}

/**
 * sessionStorage 存贮某一段时间失效
 * @param {String} key  属性
 * @param {*} value 存贮值
 * @param {String} expire 过期时间,毫秒数
 */
export const sessionStorageSetExpire = (key, value, expire) => {
	if (typeof(value) === 'object') value = JSON.stringify(value)
	sessionStorage.setItem(key, value)
	setTimeout(() => {
		sessionStorage.removeItem(key)
	}, expire)
}

/**
 * cookie 存贮
 * @param {String} key  属性
 * @param {*} value  值
 * @param String expire  过期时间,单位天
 */
export const cookieSet = (key, value, expire) => {
	const d = new Date()
	d.setDate(d.getDate() + expire)
	document.cookie = `${key}=${value};expires=${d.toGMTString()}`
}

/**
 * cookie 获取
 * @param {String} key  属性
 */
export const cookieGet = (key) => {
	const cookieStr = unescape(document.cookie)
	const arr = cookieStr.split('; ')
	let cookieValue = ''
	for (var i = 0; i < arr.length; i++) {
		const temp = arr[i].split('=')
		if (temp[0] === key) {
			cookieValue = temp[1]
			break
		}
	}
	return cookieValue
}

/**
 * cookie 删除
 * @param {String} key  属性
 */
export const cookieRemove = (key) => {
	let domain = location.host.indexOf('localhost') !== -1 ? 'localhost' : '.' + location.host
	document.cookie = `${encodeURIComponent(key)}=;expires=${new Date().toGMTString()};Domain=${domain};path=/;`
}

/**
 * cookie清空
 * @param {cookie} vue-cookies组件
 */
export const cookieClear = () => {
	let exp = new Date()
	exp.setTime(exp.getTime() - 1)
	let cookies = document.cookie.split(';')
	if (cookies.length === 0) return
	for (let i = 0; i < cookies.length; i++) {
		let cookie = cookies[i]
		let eqPos = cookie.indexOf('=')
		let name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie
		let value = name + '=;expires=' + exp.toGMTString() + ';path=/;'
		document.cookie = value
	}
}
